﻿using UnityEngine;

public class WallBlockWay : WallBlockObserver
{
    private WallWaveManager wallWaveManager;
    private int currentBlockIndex;
    private int countBlocks;

    public WallBlockWay(int _countBlocks, WallWaveManager _wallWaveManager)
    {
        countBlocks = _countBlocks;
        wallWaveManager = _wallWaveManager;
    }

    public void hasComeToEndOfWay(WallBlock block)
    {
        updateWallWaveManager();
        goToNextBlock();
        setUpNewHeightForBlock(block);
    }

    private void updateWallWaveManager()
    {
        wallWaveManager.update(currentBlockIndex);
    }

    private void goToNextBlock()
    {
        currentBlockIndex++;
        tryResetBlock();
    }

    private void tryResetBlock()
    {
        if (currentBlockIndex >= countBlocks)
            currentBlockIndex = 0;
    }

    private void setUpNewHeightForBlock(WallBlock block)
    {
        float height = getBlockHeightFromWaveManager();
        resetBlockWithNewHeight(block, height);
    }

    private float getBlockHeightFromWaveManager()
    {
        return wallWaveManager.popNextWave();
    }

    private void resetBlockWithNewHeight(WallBlock block, float height)
    {
        block.reset(height);
    }
}